﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;


using CanDoo.Core;
using CanDoo;
using CanDoo.Secutiry;
using CanDoo.Data;
using CanDoo.Web;

using CanDoo.Admin.Core.Models;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using FineUIPro;
using CanDoo.FineUI;
using CanDoo.Extensions;


namespace CanDoo.Oil.WebApp.sysAdministrator
{
    public partial class Edit : CanDoo.FineUI.EditPagebase<IAdministratorContract, Administrator>
    {

        #region PagePower
        /// <summary>
        /// 本页面的功能模块
        /// </summary>
        public override string PageTitle
        {
            get
            {
                return "系统管理员";
            }
        }

        #endregion

        private const string CONFIGPASSWORD = "%$@^HU^%W#G(%^G(";
        public IRoleContract RoleContract { get; set; }
        public IDepartmentContract DepartmentContract { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIListHelper.BindDDL(DepartmentContract.EntityName, this.DepartmentContract.GetSelectList(), 0L, ed_Department);
                FineUIBindList.BindSex(ed_Sex, true);
                FineUIBindList.BindEnable(ed_Enable, true);

                RadioRoles.DataTextField = "Name";
                RadioRoles.DataValueField = "Id";
                var result = RoleContract.GetPaged(selector: p => new { p.Id, p.Name });
                RadioRoles.DataSource = result.Data;
                RadioRoles.DataBind();

                if (PageMode == pagemode.Update || PageMode == pagemode.View)
                    LoadData();
                else
                {
                    ed_PageSize.SetValue(20);
                    ed_Enable.SetValue(true);
                }
            }
        }

        protected override void LoadData(Administrator entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_Account.SetValue(entity.User.UserName);
            ed_Sex.SetValue(entity.Sex);
            ed_Email.SetValue(entity.Email);
            ed_Address.SetValue(entity.Address);
            ed_Mobile.SetValue(entity.Mobile);
            ed_Phone.SetValue(entity.Phone);
            ed_QQ.SetValue(entity.QQ);
            ed_Birthday.SetValue(entity.Birthday);
            ed_IDCard.SetValue(entity.IDCard);
            ed_PageSize.SetValue(entity.PageSize);
            ed_Enable.SetValue(entity.User.Enable);
            ed_Remark.SetValue(entity.Remark);

            if (entity.User.Roles.Count() != 0)
                ed_Roles.Values = FineUIGridHelper.GetStringArray(entity.User.Roles);

            if (!entity.DepartmentId.IsNull())
                ed_Department.SetValue(entity.DepartmentId);

            ed_Password.SetValue(CONFIGPASSWORD);
            ed_RePassword.SetValue(CONFIGPASSWORD);
        }


        protected override OperationResult SaveData(Administrator entity)
        {
            if (PageMode == pagemode.Add)
            {
                entity.User = new User();
                entity.User.Id = EntityId;
            }
            entity.User.UserName = ed_Account.GetValue().ToString();
            entity.User.Name = ed_Name.GetValue().ToString();
            if (ed_Password.GetValue() != CONFIGPASSWORD)
            {
                entity.User.PasswordHash = PasswordHasher.HashPassword(ed_Password.GetValue());
            }

            List<Role> allRoles = RoleContract.GetAll();
            var roles = ed_Roles.Values.Select(r => allRoles.Single(p => p.Id == r.ToLong())).ToList() ?? new List<Role>();

            if (entity.User.Roles == null)
            {
                entity.User.Roles = new List<Role>();
                foreach (var role in roles)
                {
                    entity.User.Roles.Add(role);
                }
            }
            else
            {
                foreach (var role in roles.Except(entity.User.Roles))
                {
                    entity.User.Roles.Add(role);
                }
                var entityRoles = entity.User.Roles.Where(r => !roles.Contains(r)).ToList();
                foreach (var role in entityRoles)
                {
                    entity.User.Roles.Remove(role);
                }
            }

            entity.Name = ed_Name.GetValue().ToString();
            entity.Sex = ed_Sex.GetValue().ToInt();
            entity.Email = ed_Email.GetValue().ToString();
            entity.Address = ed_Address.GetValue().ToString();
            entity.Mobile = ed_Mobile.GetValue().ToString();
            entity.Phone = ed_Phone.GetValue().ToString();
            entity.QQ = ed_QQ.GetValue().ToString();
            entity.Birthday = ed_Birthday.GetValue().To<DateTime?>();
            if (entity.Birthday == default(DateTime))
                entity.Birthday = null;
            entity.IDCard = ed_IDCard.GetValue().ToString();
            entity.PageSize = ed_PageSize.GetValue().ToInt();
            entity.User.Enable = ed_Enable.GetValue().ToBool();
            entity.Remark = ed_Remark.GetValue().ToString();
            if (ed_Department.GetValue().ToLong() == 0)
                entity.DepartmentId = null;
            else
                entity.DepartmentId = ed_Department.GetValue().ToLong();


            return base.SaveData(entity);
        }
    }
}